package com.hy.service.impl;

import com.hy.bean.*;
import com.hy.dao.*;
import com.hy.dao.impl.*;
import com.hy.dto.AddInstoreDto;
import com.hy.service.InstorePartstableService;
import com.hy.utils.DBManager;
import com.hy.vo.BuyParttableVo;
import com.hy.vo.BuytableVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class InstorePartstableServiceImpl implements InstorePartstableService {
    private final BuytableDao buytableDao = new BuytableDaoImpl();
    private final BuyParttableDao buyParttableDao = new BuyParttableDaoImpl();
    private final InstoretableDao instoretableDao = new InstoretableDaoImpl();
    private final InstorePartstableDao instorePartstableDao = new InstorePartstableDaoImpl();
    private final CarpartstableDao carpartstableDao = new CarpartstableDaoImpl();

    @Override
    public RespBean addInstore(AddInstoreDto addInstoreDto){
        Connection connection = null;
        Boolean flag = false;
        Integer instoretableId = 0;
        try {
            connection = DBManager.getConnection();
            connection.setAutoCommit(false);

            //根据id查询采购单
            BuytableVo buytableVo = buytableDao.selectById(addInstoreDto.getId());

            //通过
            Instoretable getInstoretable = instoretableDao.selectByInStoreTime(buytableVo.getId());

            if (getInstoretable == null) {
                //创建入库表
                Instoretable instoretable = new Instoretable(
                        null,
                        Integer.parseInt(randomGenerate()),
                        buytableVo.getId(),
                        addInstoreDto.getNameId(),
                        new Date(),
                        null,
                        addInstoreDto.getRemark()
                );
                //添加入库并返回主键
                instoretableId = instoretableDao.insert(connection, instoretable);
            }else {
                instoretableId = getInstoretable.getId();
            }

            //采购中间表
            List<BuyParttableVo> buyParttableList = buytableVo.getChild();
            for (BuyParttableVo buyParttableVo : buyParttableList) {

                //创建入库中间表
                InstorePartstable instorePartstable = new InstorePartstable(
                        null,
                        instoretableId,
                        buyParttableVo.getParttableId(),
                        buyParttableVo.getBuycount()
                );
                //添加入库中间
                int insert = instorePartstableDao.insert(connection, instorePartstable);

                //修改库存数量
                int updatePartStore = carpartstableDao.updatePartStore(connection, buyParttableVo.getParttableId(), buyParttableVo.getBuycount());
                //修改采购数量
                int updateReachCountById = buyParttableDao.updateReachCountById(connection, buyParttableVo.getId(), buyParttableVo.getBuycount());
            }
            //修改采购人和日期
            Double zje = buyParttableDao.selectsumzj(buytableVo.getId());
            int updateBuyerAndBuyTimeById = buytableDao.updateBuyerAndBuyTimeById(connection, buytableVo.getId(), addInstoreDto.getName(), new Date(), zje);

            connection.commit();
            flag = true;
        } catch (Exception e) {
            try {
                connection.rollback();
                flag = false;
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }

        return flag ? RespBean.success("入库成功") : RespBean.error("入库失败");
    }

    private String randomGenerate() {
        String datastr = "";
        Integer randomNumber = 0;
        Integer v = (int) (Math.floor(Math.random() * (10 - 5 + 1)) + 5);
        for (int i = 0; i < v; i++) {
            if (i % 2 == 0) {
                randomNumber = (int) Math.floor(Math.random() * 10);
                datastr += randomNumber + "";
            } else {
                randomNumber = (int) Math.floor(Math.random() * 5);
                datastr += randomNumber + "";
            }
        }
        return datastr;
    }
}